<?php
namespace Models;
class LoginRedis extends RedisBase
{
    const prefix ='login_:';
    const tokenPrefix='userToken_:';
    const uidPrefix ='userId_:';
    const lifeTime= 300;
    const tokenLife =30; 
    
    /**
     * 设定验证码
     * @param string $code
     * @param string $phone
     */
    public static function setCode($code='',$phone=''){
        $redis=self::getReids();
        $key=self::prefix.$phone;
        $status=$redis->set($key,md5($code),self::lifeTime);
       return $status;
    }
    
    /**
     * 获取验证码
     * @param string $phone
     */
    public static  function getCode($phone=''){
      return   self::getReids()->get(self::prefix.$phone);
    }
    
    /**
     * 自增计数频率
     * @param string $phone
     */
    public static  function incrKey($phone=''){
       return self::getReids()->incr(self::prefix.$phone.'num');
    }
    
    /**
     * 删除验证码
     * @param string $phone
     */
    public static function delCode($phone=''){
        self::getReids()->del(self::prefix.$phone.'num');
        return self::getReids()->del(self::prefix.$phone);
    }
    
    /**
     * 存储token
     * @param string $token
     * @param string $info
     */
    public static function setToken($token='',$info=''){
        if(is_array($info)){
            $info=json_encode($info);
        }
        $redis=self::getReids();
        $time=self::tokenLife * 3600*24;
//         $redis->set(self::uidPrefix.$id,$token,$time);
        return $redis->set(self::tokenPrefix.$token,$info,$time);
    }
    
    /**
     * 检测token是否存在
     */
    public static function checkToken($token=''){
        return self::getReids()->exists(self::tokenPrefix.$token);
    }
}

?>